package com.qd.qx.ui.main.activity;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.qd.qx.R;
import com.qd.qx.api.ApiServerConstants;
import com.qd.qx.api.MyXMPPTCPConnection;
import com.qd.qx.bean.ChatMessage;
import com.qd.qx.bean.Friend;

import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.chat.Chat;
import org.jivesoftware.smack.chat.ChatManager;
import org.jivesoftware.smack.chat.ChatManagerListener;
import org.jivesoftware.smack.chat.ChatMessageListener;
import org.jivesoftware.smack.packet.Message;

import java.util.ArrayList;
import java.util.List;


/**
 * Code is far away from bug with the animal protecting
 * Author：YS on 2018/10/12
 * Function：default
 */
public class FreendsTestActivity extends Activity implements ChatMessageListener, ChatManagerListener {
    Button send;
    EditText edSend;
    MyXMPPTCPConnection connection;
    ChatManager chatManager;
    Chat chat;

    String chatId = "admin@" + ApiServerConstants.IM_URL + "/iphone";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.friendtest_activity);
        initChatManager();
        initChat();
        initView();

        List<Friend> friends = new ArrayList<Friend>();
//通过Roster.getInstanceFor(connection)获取Roast对象;
//通过Roster对象的getEntries()获取Set，遍历该Set就可以获取好友的信息了;
//        for (RosterEntry entry : Roster.getInstanceFor(connection).getEntries()) {
//            Friend friend = new Friend(entry.getUser(), entry.getName());
//            friends.add(friend);
//        }

    }


    private Handler handler = new Handler() {
        @Override
        public void handleMessage(android.os.Message msg) {
            switch (msg.what) {
                case 0:
                    ChatMessage chatMessage = new ChatMessage((String) msg.obj, 1);
                    Toast.makeText(FreendsTestActivity.this, chatMessage.getMsgContent(), Toast.LENGTH_LONG).show();
                    break;
                default:
                    break;
            }
        }
    };

    private void initChatManager() {
        connection = MyXMPPTCPConnection.getInstance();
//        if (connection != null) {
//            chatManager = ChatManager.getInstanceFor(connection);
//            chatManager.addChatListener(this);
//        }
    }

    private void initChat() {
        if (chatManager != null) {
            //第一个参数是 用户的ID
            //第二个参数是 ChatMessageListener，我们这里传null就好了
            chat = chatManager.createChat(chatId, null);
        }
    }

    private void initView() {

        send = (Button) findViewById(R.id.sendText);
        edSend = (EditText) findViewById(R.id.edSend);
        send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!TextUtils.isEmpty(edSend.getText())) {
                    sendChatMessage(edSend.getText().toString().trim());
                } else {
                    Toast.makeText(FreendsTestActivity.this, "发送内容不能为空", Toast.LENGTH_LONG).show();
                }

            }
        });
    }

    private void sendChatMessage(String msgContent) {
        ChatMessage chatMessage = new ChatMessage(msgContent, 0);
        if (chat != null) {
            try {
                //发送消息，参数为发送的消息内容
                chat.sendMessage(msgContent);
                edSend.setText("");
            } catch (SmackException.NotConnectedException e) {
                e.printStackTrace();
            }
        }
    }

    //ChatListener中需要重写的方法
    @Override
    public void chatCreated(Chat chat, boolean createdLocally) {
        //在这里面给chat对象添加ChatMessageListener
        chat.addMessageListener(this);
    }

    @Override
    public void processMessage(Chat chat, Message message) {
        if (message.getType().equals(Message.Type.chat) || message.getType().equals(Message.Type.normal)) {
            if (message.getBody() != null) {
                android.os.Message msg = android.os.Message.obtain();
                msg.what = 0;
                msg.obj = message.getBody();
                handler.sendMessage(msg);
            }
        }
    }
}
